package com.sea.finance.controller;


import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.finance.entity.VtFeeOriginally;
import com.sea.finance.service.VtFeeMatchService;
import com.sea.finance.service.VtFeeOriginallyService;
import com.sea.finance.service.VtRelatedService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


/**
 * 运费
 * @author 陈鸿飞
 */
@Controller
@RequestMapping(value = "/sea/finance/vtFeeOriginally")
public class VtFeeOriginallyController {

    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private VtFeeMatchService vtFeeMatchService;
    @Autowired
    private VtRelatedService vtRelatedService;
    @Autowired
    private VtFeeOriginallyService vtFeeOriginallyService;


    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("shop", dictDetailService.findByTypeCode("base_shop", null));
        model.addAttribute("brand", dictDetailService.findByTypeCode("base_brand", null));
        model.addAttribute("country", dictDetailService.findByTypeCode("base_country", null));
        model.addAttribute("feeType", vtFeeMatchService.findByFeeType());
        model.addAttribute("listing", vtRelatedService.findByListingName());
        return "/sea/finance/vtReport/vtFeeOriginally";
    }

    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     VtFeeOriginally vtFeeOriginally) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<VtFeeOriginally> list = vtFeeOriginallyService.findByCnd(vtFeeOriginally);
        PageInfo<VtFeeOriginally> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(VtFeeOriginally vtFeeOriginally) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (null != vtFeeOriginally && vtFeeOriginallyService.save(vtFeeOriginally) > 0) {

                //生成数据
                vtFeeOriginally=vtFeeOriginallyService.originally(vtFeeOriginally);
                if(StringUtils.isBlank(vtFeeOriginally.getFileUrl())){
                    vtFeeOriginally.setFlag(2);
                    vtFeeOriginally.setRemark("文件生成异常,请联系管理员！");
                }else{
                    vtFeeOriginally.setFlag(3);
                    vtFeeOriginally.setFileUrl(vtFeeOriginally.getFileUrl());
                }
                vtFeeOriginallyService.usave(vtFeeOriginally);
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            e.printStackTrace();
            result.setMsg("当前数据不能重复!");
            vtFeeOriginally.setFlag(2);
            vtFeeOriginally.setRemark("当前数据不能重复!");
            vtFeeOriginallyService.usave(vtFeeOriginally);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            vtFeeOriginally.setFlag(2);
            vtFeeOriginally.setRemark("系统异常,请联系管理员!");
            vtFeeOriginallyService.usave(vtFeeOriginally);
            return result;
        }
        return result;
    }

}
